﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;

namespace BT_web__nhom06
{
    public partial class Them_User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsValid == false) //neu 1 trong cac validator kiem tra khong hop le thi IsValid ==false
            {
                return;
            }
         
            string username = txtusername.Text;
            string password = txtpassword.Text;
            string mahs = txtmasv.Text;
            bool locked = ckbkhoa.Checked;
            string quyen = dlstquyen.SelectedValue.ToString();
            string caulenhsqlselect = "select * from [user] where [username]= @username";
            DataTable dtbkq = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(caulenhsqlselect, "@Username", username);
            string caulenhsqlinsert = "insert into [user] ([username],[password],[mahs],[locked],[quyen])" + " values(@username,@password,@mahs,@locked,@quyen)";
            int ketqua = Tienichsql.ketnoicsdl.chaylenhcapnhatdulieu(caulenhsqlinsert, new string[] { "@username", "@password", "@mahs", "@locked", "@quyen" }, new object[] { username, password, mahs, locked, quyen });
            if (ketqua == 1)
            {
                Label1.Text = "Da them thanh cong " + ketqua.ToString() + " user";
                txtusername.Text = "";
                txtpassword.Text = "";
                txtnhaplaipass.Text = "";
                txtmasv.Text = "";
            }
            else
                Label1.Text = "Thêm không thành công";
        }
        protected void txtusername_TextChanged(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string username = txtusername.Text;
            string sqlSelect = "SELECT * FROM [user] WHERE [Username]=@Username";
             DataTable tblKetQua = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(sqlSelect, "@Username", username);
             if (tblKetQua.Rows.Count > 0)
              args.IsValid = false;
             else
            args.IsValid = true;
         }
    }
    }
